package com.cheng.service.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.vo.SettingInfoVO;
import com.cheng.service.ComUploadService;
import com.cheng.service.SettingInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @author: ChengJW
 * @date: 2021/11/24 8:02
 * @version: 1.0
 * @description: 文件上传
 */
@Service
public class ComUploadServiceImpl implements ComUploadService {

    private final static Logger log = LoggerFactory.getLogger(ComUploadServiceImpl.class);

    @Autowired
    private SettingInfoService settingInfoService;


    /**
    * @author: ChengJW
    * @params: [file]
    * @return: java.lang.String
    * @date: 2021/11/24
    * @description:  上传文件，并回传文件存储名称
    */
    @Override
    public String uploadFile(MultipartFile file) throws CommonException {
        try {
            //获取原始的文件名称
            String origFileNm = file.getOriginalFilename();
            if (StringUtil.isNull(origFileNm)){
                log.error("上传文件的名称为空，无法进行下一步操作");
                throw new CommonException("CJW202111010052","UserInfoController","uploadPhoto");
            }
            String [] origFileNmList = StringUtil.split(origFileNm,"\\.");
            //获取文件类型
            String suffixFileNm = origFileNmList[origFileNmList.length-1];
            //重新给图片编辑名称，防止重复
            String newFileNm = UUID.randomUUID().toString()+"."+suffixFileNm;
            //获取文件存储位置
            String prefixFilePhth = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
            if (CodeUtil.FILE_CLASS_JPG.equals(suffixFileNm) || CodeUtil.FILE_CLASS_PNG.equals(suffixFileNm)){
                prefixFilePhth += CodeUtil.IMAGES_PATH;
            }else if (CodeUtil.FILE_CLASS_DOCX.equals(suffixFileNm) || CodeUtil.FILE_CLASS_DOC.equals(suffixFileNm)){
                prefixFilePhth += CodeUtil.DOCS_PATH;
            }else if (CodeUtil.FILE_CLASS_XLSX.equals(suffixFileNm) || CodeUtil.FILE_CLASS_XLS.equals(suffixFileNm)){
                prefixFilePhth += CodeUtil.EXCELS_PATH;
            }
            String filePath = null;
            SettingInfoVO settingInfoVO = settingInfoService.getSettingVO(CodeUtil.IS_OPEN_DEVELOPMENT_CD);
            //获取系统运行模式，0 生产模式；1 开发模式
            if (settingInfoVO == null || CodeUtil.DEVELOPMENT_MODEAL.equals(settingInfoVO.getSiRsrv2())){
                //开发模式，文件上传路径为相对路径
                filePath = prefixFilePhth + newFileNm;
            } else {
                //生产模式，文件上传路径为绝对路径
                String profilePhotoPath = settingInfoService.getSettingVO(CodeUtil.PROFILE_PHOTO_PATH).getSiRsrv2();
                filePath = profilePhotoPath + CodeUtil.SEPARATOR + newFileNm;
            }
            //文件上传
            file.transferTo(new File(filePath));
            //头像回显路径、用户头像存放路径为相对路径
            return  CodeUtil.IMAGES_PATH + newFileNm;
        } catch (Exception e) {
            log.error("文件上传异常:"+e.getMessage(),e);
            throw new CommonException("CJW202111010053","UserInfoController","uploadPhoto",e);
        }
    }
}
